package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.util.JsonUtil;
import com.dragon.logical.service.auth.IModifyLogService;
import com.dragon.logical.service.contract.ContractService;
import com.dragon.logical.service.customer.CustomerService;
import com.dragon.logical.service.customer.QuotesService;
import com.dragon.repository.entity.Contract;
import com.dragon.repository.entity.Customer;
import com.dragon.repository.entity.ModifyLog;
import com.dragon.repository.entity.Quotes;
import com.dragon.repository.mapper.ContractMapper;
import com.dragon.repository.mapper.CustomerMapper;
import com.dragon.repository.mapper.ModifyLogMapper;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 修改日志 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-11-20
 */
@Service
public class ModifyLogServiceImpl extends ServiceImpl<ModifyLogMapper, ModifyLog> implements IModifyLogService {

    @Autowired
    ModifyLogMapper modifyLogMapper;
    @Autowired
    QuotesService quotesService;
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    ContractService contractService;
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public Page<ModifyLog> modifyLogList(Integer page, Integer limit, Integer streamType, Integer correlationId) {
        Page<ModifyLog> logPage = new Page<>(page, limit);
        QueryWrapper<ModifyLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ModifyLog::getStreamType,streamType)
                .eq(ModifyLog::getCorrelationId,correlationId)
                .orderByDesc(ModifyLog::getFiledTime);
        Page<ModifyLog> modifyLogPage = modifyLogMapper.selectPage(logPage,wrapper);
        return modifyLogPage;
    }

    @Override
    public void modifyLogAdd(ModifyLog modifyLog) {
        String json = null;
        switch (modifyLog.getStreamType()){
            case 1:
                //报价
                Quotes quotes = quotesService.selectDetailById(modifyLog.getCorrelationId());
                json = JSON.toJSONStringWithDateFormat(quotes,"yyyy-MM-dd",SerializerFeature.WriteDateUseDateFormat);
                break;
            case 2:
                //合同
                Contract contract = contractService.selectByContractId(modifyLog.getCorrelationId());
                json = JSON.toJSONStringWithDateFormat(contract,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat);
                break;
            case 10:
                //客户
                Customer customer = customerMapper.selectById(modifyLog.getCorrelationId());
                json = JSON.toJSONStringWithDateFormat(customer,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat);
                break;
        }
        modifyLog.setFiledTime(DateUtil.date());
        modifyLog.setDateDetails(json);
        modifyLogMapper.insert(modifyLog);
    }

}
